#include <QApplication>
#include <QtQml>
#include "utils/ThreadSafeZYFPFactory.h"
#include "utils/ZYFPImageLoader.h"
#include "DataMan.h"

static QObject* reg_dataman(QQmlEngine* me, QJSEngine* jse)
{
	Q_UNUSED(me);
	Q_UNUSED(jse);
	return DataMan::instance();
}

int main(int argc, char *argv[])
{
	QApplication app(argc, argv);
	DataMan::instance()->load();
	qmlRegisterSingletonType<DataMan>("DataMan", 0, 1, "DataMan", reg_dataman);

	QQmlApplicationEngine engine;
	engine.addImageProvider("default", new ZYFPImageLoader);
#ifdef ASYNC_LOADER
	// 如果需要异步加载图片，使用这个provider
	engine.addImageProvider("asyncimg", new ZYFPImageLoader);
#endif

	engine.rootContext()->setContextProperty("GameRow", DataMan::ROW);
	engine.rootContext()->setContextProperty("GameCol", DataMan::COL);
	engine.rootContext()->setContextProperty("PropTypeN", DataMan::PropTypeN);
	engine.rootContext()->setContextProperty("MonsterTypeN", DataMan::MonsterTypeN);

	engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));

	int r = app.exec();
	// 最后一定要释放资源
	ThreadSafeZYFPFactory::end();
	DataMan::instance()->save();
	return r;
}
